package com.sxtyxt.online;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sxtyxt.entity.ResultVo;
import com.sxtyxt.entity.User;
import com.sxtyxt.util.GsonUtil;
import com.sxtyxt.util.ProgressUtil;
import com.sxtyxt.util.UIUtil;
import com.sxtyxt.volley.ResultListenerImpl;
import com.sxtyxt.volley.URL;
import com.sxtyxt.volley.VolleyUtils;


/**
 * 登录页面
 *
 * @author General
 */
public class ActLogin extends ActBase implements OnClickListener, Handler.Callback {
    /**
     * 用户名和密码
     */
    private EditText etxtUserName, etxtPwd;
    /**
     * 登陆按钮
     */
    private Button btnLogin;
    /**
     * 找回密码
     */
    private TextView etxtFindPwd;
    /**
     * 注册
     */
    TextView txtRegister;
    private String pkId;

    @Override
    protected int getLayoutId() {
        return R.layout.login;
    }

    @Override
    protected void initUI() {
        super.initUI();
        etxtUserName = (EditText) findViewById(R.id.etxt_username);
        etxtPwd = (EditText) findViewById(R.id.etxt_pwd);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        etxtUserName.setText(getUnNullString(SystemEnv.getRememberUserName(), ""));
        UIUtil.screenCopyAndPaste(etxtPwd);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        etxtPwd.setText("");
    }


    /**
     * 处理操作结果
     * <p/>
     * 如果获取到用户的名称，则显示名称；否则如果已经授权，则显示 平台名称
     */
    public boolean handleMessage(Message msg) {

        ProgressUtil.hide();
        switch (msg.arg1) {
            case 3:
                // 取消
                doToast("授权取消");
                return false;
        }

        return false;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_login:
                System.out.println("btnLogin..");
                if (TextUtils.isEmpty(getStringByUI(etxtUserName))) {
                    doToast("用户名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(getStringByUI(etxtPwd))) {
                    doToast("密码不能为空");
                    return;
                }


                ProgressUtil.show(this, "正在登录，请稍候");
                VolleyUtils.requestService(SystemConst.LOGIN_URL,
                        URL.getLoginParams("1", getStringByUI(etxtUserName), getStringByUI(etxtPwd)),
                        new ResultListenerImpl(this) {

                            @Override
                            public void onSuccess(String response) {
                                super.onSuccess(response);
                                LoginVo vo = GsonUtil.deser(response, LoginVo.class);
                                if (vo == null) {
                                    ProgressUtil.hide();
                                    doToast(R.string.msg_wso_error);
                                    return;
                                }
                                if (vo.getResult() == 1) {
                                    if (vo.getList() != null && vo.getList().size() > 0) {
                                        User user = vo.getList().get(0);
                                        pkId = user.getPkId();
                                        if (user != null) {
                                            SystemEnv.saveUser(user);
                                            SystemEnv.rememberUserNameAndPassword(getStringByUI(etxtUserName), getStringByUI(etxtPwd));
                                            App.getInstance().login(vo.getList().get(0));
                                            transfer(ActMains.class);
                                            finish();
                                        } else {
                                            doToast("登录失败，请重试");
                                            ProgressUtil.hide();

                                        }
                                    } else {
                                        doToast("登录失败，请重试");
                                        ProgressUtil.hide();
                                    }
                                } else {
                                    doToast(vo.getMsg());
                                    ProgressUtil.hide();
                                }
                            }

                            @Override
                            public void onError() {
                                super.onError();
                                ProgressUtil.hide();
                            }

                        });

                break;


            default:
                break;
        }
    }

    public String getPkId() {
        return getLoginUser().getPkId();
    }

    class LoginVo extends ResultVo<User> {

        /**
         *
         */
        private static final long serialVersionUID = 1L;

    }


    class MemberVo extends ResultVo<User> {

    }
}



